#include "usart7.h"

#define T_F 0

#define USART2_TX_BUFFER_SIZE 0
#define USART2_RX_BUFFER_SIZE 20U

void dma_configuration(void);
u8 Usart7_RXBUF[UART7_BUFLEN];



/*******************************保留**********************************/





void Uart7_Init(uint32_t bound)
{
  gpio_init_type gpio_init_struct;
  dma_init_type dma_init_struct;
  
  crm_periph_clock_enable(CRM_UART7_PERIPH_CLOCK, TRUE);   //开启USART1的时钟
	crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
  crm_periph_clock_enable(UART7_GPIO_CRM_CLK, TRUE);
	
  gpio_default_para_init(&gpio_init_struct);

  /* configure the usart7 tx pin */
  gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  gpio_init_struct.gpio_out_type  = GPIO_OUTPUT_PUSH_PULL;
  gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
  gpio_init_struct.gpio_pins = UART7_TX_PIN|UART7_RX_PIN;
  gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  gpio_init(UART7_GPIO, &gpio_init_struct);

	gpio_pin_mux_config(UART7_GPIO, GPIO_PINS_SOURCE3, UART7_PIN_MUX_NUM);//复用IO为串口接收
	gpio_pin_mux_config(UART7_GPIO, GPIO_PINS_SOURCE4, UART7_PIN_MUX_NUM);//服用IO为串口发送


	//DMA基础配置
  crm_periph_clock_enable(CRM_DMA1_PERIPH_CLOCK, TRUE);
  dmamux_enable(DMA1, TRUE);//启用或禁用 DMAMUX。
  dma_reset(DMA1_CHANNEL3);
  dma_default_para_init(&dma_init_struct);
  dma_init_struct.buffer_size = UART7_BUFLEN;                     					//内存大小
  dma_init_struct.direction = DMA_DIR_PERIPHERAL_TO_MEMORY;                	//外设地址为目的地址
  dma_init_struct.memory_base_addr = (uint32_t)&Usart7_RXBUF;               //内存地址
  dma_init_struct.memory_data_width = DMA_MEMORY_DATA_WIDTH_BYTE;          	//内存数据的宽度 8bit
  dma_init_struct.memory_inc_enable = TRUE;                                	//内存地址递增打开
  dma_init_struct.peripheral_base_addr = (uint32_t)&UART7->dt;            	//外设地址
  dma_init_struct.peripheral_data_width = DMA_PERIPHERAL_DATA_WIDTH_BYTE;  	//外设数据的宽度 8bit
  dma_init_struct.peripheral_inc_enable = FALSE;                           	//外设地址递增关闭
  dma_init_struct.priority = DMA_PRIORITY_MEDIUM;                          	//中等优先级
  dma_init_struct.loop_mode_enable = TRUE;                                	//使能
  dma_init(DMA1_CHANNEL3, &dma_init_struct);

	dma_flexible_config(DMA1,DMA1MUX_CHANNEL3,DMAMUX_DMAREQ_ID_UART7_RX);  //DMA1的通道2设定为串口2的DMA接收通道
	usart_dma_receiver_enable(UART7, TRUE);                           //使能DMA接收
	dma_channel_enable(DMA1_CHANNEL3, TRUE); 													// usart7 rx 开始 DMA 接收 

	
	
	//USART7配置
  nvic_irq_enable(UART7_IRQn, usart7_preempt_priority, usart7_sub_priority);                               	//使能串口1终端，优先级0，次优先级0
	/* configure usart2 param */
  usart_init(UART7, bound, USART_DATA_8BITS, USART_STOP_1_BIT);
  usart_transmitter_enable(UART7, TRUE);
  usart_receiver_enable(UART7, TRUE);
  usart_interrupt_enable(UART7, USART_IDLE_INT, TRUE);
  usart_enable(UART7, TRUE);

}


void UART7_IRQHandler(void)
{
	 uint8_t clear;
	
	 BaseType_t xHigherPriorityTaskWoken = pdFALSE;
	
	 if(usart_flag_get(UART7, USART_IDLEF_FLAG) != RESET)                   // USART7总线空闲
		 {
			 usart_data_receive(UART7);  																				//clear idle flag;
			 dma_channel_enable(DMA1_CHANNEL3, FALSE);     											//关闭DMA通道
			 dma_data_number_set(DMA1_CHANNEL3, UART7_BUFLEN);
			 xSemaphoreGiveFromISR(Usart4_sem,&xHigherPriorityTaskWoken);				//发送信号量
		 }
}

void Uart7Send(uint8_t *buff,uint8_t size)
{
	uint8_t *Enter_click = buff;
	
	for (uint8_t uart7_tx_cnt = 0; uart7_tx_cnt < size; uart7_tx_cnt++)
	{
		while (usart_flag_get(UART7, USART_TDBE_FLAG) == RESET)
			;
		usart_data_transmit(UART7, Enter_click[uart7_tx_cnt]);
		while (usart_flag_get(UART7, USART_TDC_FLAG) == RESET)
			;
	}
}





